//
//  ChangePwdViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/20.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "InputTwoView.h"
@interface ChangePwdViewController ()

/** 原密码 */
@property (nonatomic,strong) UITextField *orginPwdTF;
/** 新密码 */
@property (nonatomic,strong) UITextField *newsPwdTF;
/** 确认新密码 */
@property (nonatomic,strong) UITextField *surePwdTF;

@end

@implementation ChangePwdViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改登录密码";
    [self setUpUI];
}

- (void)setUpUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 1);
    scrollView.backgroundColor = WB_RGBColor(240, 240, 240);
    [self.view addSubview:scrollView];
    
    //手机号码
    InputTwoView *phoneView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    phoneView.titleLb.text = @"登录手机号";
    phoneView.inputTF.enabled = NO;
    phoneView.frame = CGRectMake(0, 10, kScreenWidth, 45);
    phoneView.inputTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:AppUserPhone];
    [scrollView addSubview:phoneView];
    
    //旧密码
    InputTwoView *oldPwdView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    oldPwdView.titleLb.text = @"旧密码";
    oldPwdView.frame = CGRectMake(0, phoneView.mm_bottom, kScreenWidth, 45);
    oldPwdView.inputTF.placeholder = @"请填写旧密码";
    oldPwdView.inputTF.secureTextEntry = YES;
    _orginPwdTF = oldPwdView.inputTF;
    [scrollView addSubview:oldPwdView];
    
    //新密码
    InputTwoView *newPwdView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    newPwdView.titleLb.text = @"新密码";
    newPwdView.frame = CGRectMake(0, oldPwdView.mm_bottom, kScreenWidth, 45);
    newPwdView.inputTF.secureTextEntry = YES;
    newPwdView.inputTF.placeholder = @"请填写新密码";
    _newsPwdTF = newPwdView.inputTF;
    [scrollView addSubview:newPwdView];
    
    //确认密码
    InputTwoView *surePwdView = [[NSBundle mainBundle] loadNibNamed:@"InputTwoView" owner:self options:nil].firstObject;
    surePwdView.titleLb.text = @"确认密码";
    surePwdView.inputTF.secureTextEntry = YES;
    surePwdView.frame = CGRectMake(0, newPwdView.mm_bottom, kScreenWidth, 45);
    surePwdView.inputTF.placeholder = @"请再次输入新密码";
    _surePwdTF = surePwdView.inputTF;
    [scrollView addSubview:surePwdView];
    
    //提示
    UILabel *tipLb = [[UILabel alloc] initWithFrame:CGRectMake(15, surePwdView.mm_bottom + 15, kScreenWidth - 30, 20)];
    tipLb.text = @"密码必须为6～16个字符,而且同时包含字母和数字";
    tipLb.font = [UIFont systemFontOfSize:12];
    tipLb.textColor = WB_RGBColor(153, 153, 153);
    [scrollView addSubview:tipLb];
    
    //确定
    UIButton *sureBtn = [Factory customButtonWithTitle:@"确定"];
    sureBtn.tag = 1000;
    sureBtn.frame = CGRectMake(60, tipLb.mm_bottom + 25, kScreenWidth - 120, 60);
    [sureBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:sureBtn];
}

- (void)clickMethod:(UIButton *)sender
{
    WB_WeakSelf(self)
    if (WB_IsNilString(_orginPwdTF.text)) {
        [NSObject wb_showAlertWithTitle:@"请输入密码" duration:TipTime];
        return;
    }else if (WB_IsNilString(_newsPwdTF.text)){
        [NSObject wb_showAlertWithTitle:@"请输入新密码" duration:TipTime];
        return;
    }else if (WB_IsNilString(_surePwdTF.text)){
        [NSObject wb_showAlertWithTitle:@"请再次输入新密码" duration:TipTime];
        return;
    }else if ([_orginPwdTF.text isEqualToString:_newsPwdTF.text]){
        [NSObject wb_showAlertWithTitle:@"原密码不能与新密码相同" duration:TipTime];
        return;
    }else if (_orginPwdTF.text.length < 6 || _newsPwdTF.text.length < 6 || _surePwdTF.text.length < 6){
        [NSObject wb_showAlertWithTitle:@"密码必须大于6位" duration:TipTime];
        return;
    }else if (![_surePwdTF.text isEqualToString:_newsPwdTF.text]){
        [NSObject wb_showAlertWithTitle:@"两次输入的密码不一致" duration:TipTime];
        return;
    }
    
    [XYHttpRequest dataRequestWithNewPwd:_newsPwdTF.text oldPwd:_orginPwdTF.text success:^(id responseObj) {
        [NSObject wb_showAlertWithTitle:responseObj[@"info"] duration:1];
        if (Success) {
            //延时1s返回
            WB_DISPATCH_MAIN_Delay_THREAD(1, ^{
                [weakself.navigationController popViewControllerAnimated:YES];
            })
        }
    } failure:^(NSError *error) {
        
    }];
    
}



@end
